<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="批次" prop="batch">
        <el-input
            v-model="queryParams.batch"
            clearable
            placeholder="请输入批次"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['chickhatchinginfo:chickhatchinginfo:add']"
            icon="Plus"
            plain
            type="primary"
            @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['chickhatchinginfo:chickhatchinginfo:edit']"
            :disabled="single"
            icon="Edit"
            plain
            type="success"
            @click="handleUpdate"
        >编辑
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['chickhatchinginfo:chickhatchinginfo:remove']"
            :disabled="multiple"
            icon="Delete"
            plain
            type="danger"
            @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['chickhatchinginfo:chickhatchinginfo:export']"
            icon="Download"
            plain
            type="warning"
            @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="chickhatchinginfoList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="序号" type="index" width="50px"></el-table-column>
      <el-table-column align="center" label="批次" prop="batch" width="80px"/>
      <el-table-column align="center" label="设备ID" prop="deviceId"/>
      <el-table-column align="center" label="经手人" prop="handler"/>
      <el-table-column align="center" label="品种" prop="breed" width="80px"/>
      <el-table-column align="center" label="孵蛋数量" prop="eggsCount"/>
      <el-table-column align="center" label="孵蛋开始时间" prop="hatchingStartTime" width="105px">
        <template #default="scope">
          <span>{{ parseTime(scope.row.hatchingStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="照蛋时间" prop="candleTime" width="100px">
        <template #default="scope">
          <span>{{ parseTime(scope.row.candleTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="无精蛋数量" prop="infertileEggs" width="100%"/>
      <el-table-column align="center" label="受精率" prop="fertilizationRate" width="80px"/>
      <el-table-column align="center" label="喷水凉蛋时间" prop="coolingTime" width="110px">
        <template #default="scope">
          <span>{{ parseTime(scope.row.coolingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="落盘时间" prop="trayDropTime" width="110">
        <template #default="scope">
          <span>{{ parseTime(scope.row.trayDropTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="计划出壳时间" prop="expectedHatchTime" width="110">
        <template #default="scope">
          <span>{{ parseTime(scope.row.expectedHatchTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="出壳数量" prop="hatchedCount" width="80px"/>
      <el-table-column align="center" label="在养数量" prop="inCareCount" width="80px"/>
      <el-table-column align="center" label="出壳率" prop="hatchRate" width="80px"/>
      <el-table-column align="center" label="说明" prop="description" width="80px"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="130px">
        <template #default="scope">
          <el-button v-hasPermi="['chickhatchinginfo:chickhatchinginfo:edit']" icon="Edit" link type="primary"
                     @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button v-hasPermi="['chickhatchinginfo:chickhatchinginfo:remove']" icon="Delete" link type="primary"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        :total="total"
        @pagination="getList"
    />

    <!-- 添加或修改小鸡孵化信息对话框 -->
    <el-dialog v-model="open" :title="title" append-to-body width="700px">
      <el-form ref="chickhatchinginfoRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="批次" prop="batch" style="width: 100%">
              <el-input v-model="form.batch" placeholder="请输入批次"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="设备ID" prop="deviceId">
              <el-input v-model="form.deviceId" placeholder="请输入设备ID"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="经手人" prop="handler">
              <el-input v-model="form.handler" placeholder="请输入经手人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="品种" prop="breed">
              <el-input v-model="form.breed" placeholder="请输入品种"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="孵蛋数量" prop="eggsCount">
              <el-input v-model="form.eggsCount" placeholder="请输入孵蛋数量"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="孵蛋开始" prop="hatchingStartTime">
              <el-date-picker v-model="form.hatchingStartTime"
                              clearable
                              placeholder="请选择孵蛋开始时间"
                              type="date"
                              value-format="YYYY-MM-DD">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="照蛋时间" prop="candleTime">
              <el-date-picker v-model="form.candleTime"
                              clearable
                              placeholder="请选择照蛋时间"
                              type="date"
                              value-format="YYYY-MM-DD">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="无精蛋数量" prop="infertileEggs">
              <el-input v-model="form.infertileEggs" placeholder="请输入无精蛋数量"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="受精率" prop="fertilizationRate">
              <el-input v-model="form.fertilizationRate" placeholder="请输入受精率"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="凉蛋时间" prop="coolingTime">
              <el-date-picker v-model="form.coolingTime"
                              clearable
                              placeholder="请选择喷水凉蛋时间"
                              type="date"
                              value-format="YYYY-MM-DD">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="落盘时间" prop="trayDropTime">
              <el-date-picker v-model="form.trayDropTime"
                              clearable
                              placeholder="请选择落盘时间"
                              type="date"
                              value-format="YYYY-MM-DD">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="预出壳时间" prop="expectedHatchTime">
              <el-date-picker v-model="form.expectedHatchTime"
                              clearable
                              placeholder="请选择计划出壳时间"
                              type="date"
                              value-format="YYYY-MM-DD">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="出壳数量" prop="hatchedCount">
              <el-input v-model="form.hatchedCount" placeholder="请输入出壳数量"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="在养数量" prop="inCareCount">
              <el-input v-model="form.inCareCount" placeholder="请输入在养数量"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="出壳率" prop="hatchRate">
              <el-input v-model="form.hatchRate" placeholder="请输入出壳率"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="说明" prop="description">
              <el-input v-model="form.description" placeholder="请输入说明"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script name="Chickhatchinginfo" setup>
import {
  addChickhatchinginfo,
  delChickhatchinginfo,
  getChickhatchinginfo,
  listChickhatchinginfo,
  updateChickhatchinginfo
} from "@/api/chickhatchinginfo/chickhatchinginfo";

const {proxy} = getCurrentInstance();

const chickhatchinginfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    batch: null,
  },
  rules: {
    batch: [
      {required: true, message: "批次不能为空", trigger: "blur"}
    ],
    deviceId: [
      {required: true, message: "设备ID不能为空", trigger: "blur"}
    ],
    handler: [
      {required: true, message: "经手人不能为空", trigger: "blur"}
    ],
    breed: [
      {required: true, message: "品种不能为空", trigger: "blur"}
    ],
    eggsCount: [
      {required: true, message: "孵蛋数量不能为空", trigger: "blur"}
    ],
    hatchingStartTime: [
      {required: true, message: "孵蛋开始时间不能为空", trigger: "blur"}
    ],
    candleTime: [
      {required: true, message: "照蛋时间不能为空", trigger: "blur"}
    ],
    infertileEggs: [
      {required: true, message: "无精蛋数量不能为空", trigger: "blur"}
    ],
    fertilizationRate: [
      {required: true, message: "受精率不能为空", trigger: "blur"}
    ],
    coolingTime: [
      {required: true, message: "喷水凉蛋时间不能为空", trigger: "blur"}
    ],
    trayDropTime: [
      {required: true, message: "落盘时间不能为空", trigger: "blur"}
    ],
    expectedHatchTime: [
      {required: true, message: "计划出壳时间不能为空", trigger: "blur"}
    ],
    hatchedCount: [
      {required: true, message: "出壳数量不能为空", trigger: "blur"}
    ],
    inCareCount: [
      {required: true, message: "在养数量不能为空", trigger: "blur"}
    ],
    hatchRate: [
      {required: true, message: "出壳率不能为空", trigger: "blur"}
    ],
    description: [
      {required: true, message: "说明不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询小鸡孵化信息列表 */
function getList() {
  loading.value = true;
  listChickhatchinginfo(queryParams.value).then(response => {
    chickhatchinginfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    tenantId: null,
    batch: null,
    deviceId: null,
    handler: null,
    breed: null,
    eggsCount: null,
    hatchingStartTime: null,
    candleTime: null,
    infertileEggs: null,
    fertilizationRate: null,
    coolingTime: null,
    trayDropTime: null,
    expectedHatchTime: null,
    hatchedCount: null,
    inCareCount: null,
    hatchRate: null,
    description: null,
    createdBy: null,
    updatedBy: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("chickhatchinginfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加小鸡孵化信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getChickhatchinginfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改小鸡孵化信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["chickhatchinginfoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateChickhatchinginfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addChickhatchinginfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除小鸡孵化信息编号为"' + _ids + '"的数据项？').then(function () {
    return delChickhatchinginfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('chickhatchinginfo/chickhatchinginfo/export', {
    ...queryParams.value
  }, `chickhatchinginfo_${new Date().getTime()}.xlsx`)
}

getList();
</script>
<style scoped>
.input-column {
  width: calc(50% - 10px); /* 考虑到gutter的影响，这里减去10px */
}

.input-column .el-input__inner,
.input-column .el-date-editor .el-input__inner {
  width: 100% !important; /* 确保所有输入框的宽度为100% */
  height: 36px; /* 统一输入框高度 */
}

/* 由于文本区域的默认样式可能不同，需要特别处理 */
.input-column .el-textarea__inner {
  width: 100% !important;
  height: 90px; /* 或者你希望的高度 */
}
</style>
